import re
import wmi


def map_drive_letter(path):  # BK_01:\XXX, G:\XXX, \\diskstation\XXX
    if re.match(r'^[a-zA-Z]:[\\/]', path):  # 常规盘符
        return path
    elif re.match(r'^[^:]+:[\\/]', path):  # 卷标
        match = re.match(r'^([^:]+):([\\/])(.*)$', path)
        volume_label = match.group(1)
        c = wmi.WMI()
        # 遍历所有逻辑磁盘
        for disk in c.Win32_LogicalDisk():
            if disk.VolumeName == volume_label:
                drive_letter = disk.DeviceID  # 例如 "G:"
                return path.replace(f"{volume_label}:", drive_letter)
        raise Exception(f"未找到卷标为 '{volume_label}' 的驱动器")
    else:  # 网络盘符
        return path


if __name__ == "__main__":
    # result = get_drive_letter("BK_01:/06.Cartoons")
    result = map_drive_letter("//diskstation/Cartoons&Docs/06.Cartoons_unmanaged")
    print(result)